package com.lanou.fooddemo.listener;

import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.Poi;
import com.lanou.fooddemo.util.NetUtil;

import java.util.List;

/**
 * Created by dllo on 2015/8/3.
 */
public class FoodLocationListener implements BDLocationListener {
    private static final String LOG_TAG = "FoodLocationListener";
    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        StringBuffer sb = new StringBuffer();
        if (BDLocation.TypeNetWorkLocation == bdLocation.getLocType()) {
            sb.append("\naddr : ");

            //将Map返回的城市名转为不带“市”字的城市名
            String city = bdLocation.getCity();
            city = getCityOfMapCity(city);
            NetUtil.CITY = city;
            sb.append(city);

            sb.append("\noperationers : ");
            sb.append(bdLocation.getOperators());
            sb.append("\ndescribe : ");
            sb.append("网络定位成功");
        } else if (bdLocation.getLocType() == BDLocation.TypeServerError) {
            sb.append("\ndescribe : ");
            sb.append("服务端网络定位失败，可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com，会有人追查原因");
         } else if (bdLocation.getLocType() == BDLocation.TypeNetWorkException) {
            sb.append("\ndescribe : ");
            sb.append("网络不同导致定位失败，请检查网络是否通畅");
        } else if (bdLocation.getLocType() == BDLocation.TypeCriteriaException) {
            sb.append("\ndescribe : ");
            sb.append("无法获取有效定位依据导致定位失败，一般是由于手机的原因，处于飞行模式下一般会造成这种结果，可以试着重启手机");
        }
        sb.append("\nlocationdescribe : ");
        sb.append(bdLocation.getLocationDescribe());// 位置语义化信息
        List<Poi> list = bdLocation.getPoiList();// POI数据
        if (list != null) {
            sb.append("\npoilist size = : ");
            sb.append(list.size());
            for (Poi p : list) {
                sb.append("\npoi= : ");
                sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
            }
        }
        Log.i(LOG_TAG, sb.toString());
    }

    /**
     * 将带“市”字的城市名转为不带“市”字
     * @param city 百度地图返回的城市名
     * @return 不带市字的城市名
     */
    private String getCityOfMapCity(String city) {
        return city.split("市")[0];
    }
}
